package com.zhuang.taiqiu.netty.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuang.taiqiu.config.GCmd;
import com.zhuang.taiqiu.config.SessionManage;
import com.zhuang.taiqiu.config.UserState;
import com.zhuang.taiqiu.model.*;
import com.zhuang.taiqiu.model.vo.PlayerVO;
import com.zhuang.taiqiu.proto.GameMessageOuterClass.GameMessage;
import com.zhuang.taiqiu.utils.NettyUtil;
import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 * 处理登录的处理器
 */
@Component
public class LoginHandler {


    //处理
    public void doLogin(ChannelHandlerContext ctx, GameMessage msg){

        JSONObject jsonParse = JSON.parseObject(msg.getData());
//        String userId = jsonParse.getString("userId");
//        String userName = jsonParse.getString("userName");
//        FrameInfo frameInfo = new FrameInfo();

        PlayerVO playerVO = isLogin(ctx, jsonParse);
        //判断是否已经登录，判断是否已经注册
        if (null == playerVO && !isRegister(ctx, jsonParse)) {
            NettyUtil.sendMsg(ctx.channel(), playerVO, GCmd.S2C.USER_LOGIN_FAIL.getDescription());
            return;
        }

//        String[] playerList = {userId};
//        String[] watcherList = {};
//
//        frameInfo.setCurrentPlayer(userName);
//        frameInfo.setPlayerList(playerList);
//        frameInfo.setWatcherList(watcherList);




        NettyUtil.sendMsg(ctx.channel(), playerVO, GCmd.S2C.USER_LOGIN_SUCCESS.getDescription());
    }

    private boolean isRegister(ChannelHandlerContext ctx, JSONObject jsonParse) {
        /**获取用户资料，进行注册*/
        String userName = jsonParse.getString("userName");
        if (StringUtils.isNotBlank(userName)) {
            String userId = jsonParse.getString("userId");
            User user = new User();
            user.setId(Long.parseLong(userId));
            SessionManage.bindChannel(user, ctx.channel());
            return true;
        }
        return false;
    }

    private PlayerVO isLogin(ChannelHandlerContext ctx, JSONObject jsonParse) {
        /**验证token是否登录成果*/
        String token = jsonParse.getString("token");
        if (StringUtils.isNotBlank(token)) {
            /**验证token的合法性，如果合法未过期，代表成功**/
            /**这里的代码，暂时就不写了==============**/
            String userId = jsonParse.getString("userId");
            String userName = jsonParse.getString("userName");
            String imgUrl = jsonParse.getString("imgUrl");
            PlayerVO playerVO = new PlayerVO();
            User user = new User();
            user.setId(Long.parseLong(userId));
            user.setUserName(userName);
            user.setImgUrl(imgUrl);
            user.setState(UserState.OFFLINE_STATE);
            BeanUtils.copyProperties(user, playerVO);
            playerVO.setUserId(user.getId());
            SessionManage.bindChannel(user, ctx.channel());
            return playerVO;
        }
        return null;/**尚未登录或登录失败**/
    }


}
